<?php
class Cate{
	private $db;							//数据库连接
	private $main_table='data_cate';			//主表
	private $main_id='cate_id';				//主id
	private $max_level=3;					//最大级别
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	/**
	* 	insert menu
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		if($data['cate_level']> $this->max_level)
		my_jump('系统已限制最大'.$this->max_level.'层结构');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	/**
	* 	update menu
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		if($data['cate_level']> $this->max_level)
		my_jump('系统已限制最大'.$this->max_level.'层结构');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	
	/**
	* 	delete menu
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data($where){
		$result = $this->db->delete($this->main_table,$where);
		return $result;
	}
	


	/**
	* 	set menu (hot display etc.)
	*	@param string $default 
	*	@param string $type 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function set_data($default='',$type,$where){
		if(!$type){
			my_jump('请指定更新类型');
		}
		$set = array();
		switch($type){
			case 'common':
				if($default=='1')
				$set = array('cate_common'=>'0');
				else
				$set = array('cate_common'=>'1');
			break;
			default:
				my_jump('输入的参数有误');
			break;
		}
		$result = $this->db->update($this->main_table,$set,$where);
		return $result;
	}
	
	
	/**
	* 	get menu info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_data_row($id,$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE cate_id='".$id."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	
	/**
	* 	get menu array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='cate_id',$limit='0',$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}
	
	/**
	* 	get loop menu data 用递归生成多维数组
	* 	@param int $parent_id
	*	@param array $data
	* 	@return array $data 
	*/
	public function get_tree($all='1',$parent_id="0"){
		$sql  = "select cate_id ,cate_title,cate_pid,cate_order,cate_show from data_cate ";	
		if(!$all){
			$sql .=" where cate_show='1'";
		}
		$sql .=" order by cate_order";
		$data = $this->db->getAll($sql);
		if($data){
			$newdata = loop_data($parent_id,$data,'cate_pid','cate_id');
			return $newdata;
		}
	} 

	public function get_cate_option($pid='0'){
		$sql  = "select cate_id ,cate_title from data_cate where cate_pid='".intval($pid)."' order by cate_order ";	
		$data = $this->db->getAll($sql);
		if($data){
			$newdata = change_array($data,'cate_id','cate_title');
			return $newdata;
		}
		
	}


	public function update_data_file(){
		$sql  = "select cate_id as id,cate_title as title,cate_pid as pid from data_cate where cate_show='1' order by cate_order";	
		$data = $this->db->getAll($sql);
		if($data){
			$newdata = loop_data(0,$data,'pid','id');
			$new_array = var_export($newdata,true);
			$input_data= "\$cf_var_cate=".$new_array;
			write_file(INFO_PATH."cf_var_cate".$type.".php",$input_data);
			
			$newdata2 = loop_data(0,$data,'pid','id',0);
			$json = json_encode($newdata2);
			write_file(INFO_PATH."cf_var_cate".$type.".js",$json,"js");
			
		}
		
		/*$sql  = "select cate_id as id,cate_title as title,cate_pid as pid from data_cate where cate_show='1' and cate_pid='3' order by cate_order";	
		$data2 = $this->db->getAll($sql);
		if($data2){
			$new_array = var_export($data2,true);
			$input_data= "\$cf_var_brand=".$new_array;
			write_file(INFO_PATH."cf_var_brand".$type.".php",$input_data);
		}*/
		
	}


	public function get_cate_power($plist){
		if($plist){
			require_once(INFO_PATH.'cf_var_cate.php');
			$array = explode(",",$plist);
			$list = '';
			foreach($array as $v){
				$list .= $cf_var_cate[$v]['title'].' ';		
			}
		}
		return $list;
	}
	
	public function check_cate_power($cate,$plist){
		if($plist){
			$array = explode(",",$plist);	
			if(in_array($cate,$array)){
				return true;	
			}else{
				return false;	
			}
		}else{
			return true;	
		}
		
		
	}
	
}

?>